package cn.ujn.medicalbackend.service.impl;

import cn.ujn.medicalbackend.common.ErrorCode;
import cn.ujn.medicalbackend.domain.Drugcompany;
import cn.ujn.medicalbackend.exception.BusinessException;
import cn.ujn.medicalbackend.request.DrugcompanyAddRequest;
import cn.ujn.medicalbackend.request.DrugcompanyUpdateRequest;
import cn.ujn.medicalbackend.request.DrugcompanyDeleteRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ujn.medicalbackend.service.DrugcompanyService;
import cn.ujn.medicalbackend.mapper.DrugcompanyMapper;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author 15926
* @description 针对表【drugcompany】的数据库操作Service实现
* @createDate 2024-10-25 18:49:07
*/
@Service
public class DrugcompanyServiceImpl extends ServiceImpl<DrugcompanyMapper, Drugcompany>
    implements DrugcompanyService {

    @Override
    public Page<Drugcompany> DrugcompanyPageTransfer(Page<Drugcompany> tempPage) {
        return tempPage;
    }

    @Override
    public long addDrugcompany(DrugcompanyAddRequest drugcompanyAddRequest, HttpServletRequest request) {
        Drugcompany drugcompany = new Drugcompany();
        BeanUtils.copyProperties(drugcompanyAddRequest, drugcompany);
        System.out.println(drugcompany.toString());
        Date now = new Date();
        drugcompany.setCreatetime(now);
        drugcompany.setUpdatetime(now);
        // 3.校验信息
        if (drugcompany.getCompanyName() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "公司名不能为空");
        }
        boolean result;
        result = save(drugcompany);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "添加公司失败");
        }

        return drugcompany.getCompanyId();
    }

    @Override
    public boolean updateDrugcompany(DrugcompanyUpdateRequest drugcompanyUpdateRequest, HttpServletRequest request) {
        if (drugcompanyUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Date now = new Date();
        Drugcompany drugcompany =getById(drugcompanyUpdateRequest.getCompanyId());
        if(drugcompany==null){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "该公司不存在，更新失败");
        }
        BeanUtils.copyProperties(drugcompanyUpdateRequest, drugcompany);
        drugcompany.setUpdatetime(now);

        boolean result =updateById(drugcompany);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "更新失败");
        }

        return true;
    }

    @Override
    public boolean deleteDrugcompany(DrugcompanyDeleteRequest drugcompanyDeleteRequest, HttpServletRequest request) {
        Long drugcompanyId = drugcompanyDeleteRequest.getCompanyId();

        if (drugcompanyId == null ) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "；公司id和公司名不能为空");
        }
        QueryWrapper<Drugcompany> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id", drugcompanyId);
        long count = count(queryWrapper);
        if (count <= 0) {
            throw new BusinessException(ErrorCode.NULL_ERROR, "没有该公司");
        }

        boolean result = removeById(drugcompanyId);
        if (!result) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "删除失败");
        }
        return true;
    }


}



